project (OSCam C)

#----------------------- minimum version of cmake to use ------------

cmake_minimum_required(VERSION 2.8.12)

cmake_policy(SET CMP0003 NEW)
if(POLICY CMP0042)
  cmake_policy(SET CMP0042 NEW)
endif()
if(POLICY CMP0054)
  cmake_policy(SET CMP0054 NEW)
endif()

#----------------------- detect system ------------------------------

if (CMAKE_CROSSCOMPILING)
	if (OSCAM_SYSTEM_NAME MATCHES "Tuxbox")
		set (OSCamOperatingSystem "Tuxbox")
	elseif (OSCAM_SYSTEM_NAME MATCHES "TripleDragon")
		set (OSCamOperatingSystem "TripleDragon")
	elseif (OSCAM_SYSTEM_NAME MATCHES "Coolstream")
		set (OSCamOperatingSystem "NeutrinoHD")
	elseif (OSCAM_SYSTEM_NAME MATCHES "CST2")
		set (OSCamOperatingSystem "NHD2")
	elseif (OSCAM_SYSTEM_NAME MATCHES "NSLU2")
		set (OSCamOperatingSystem "SlugOS")
	elseif (OSCAM_SYSTEM_NAME MATCHES "WRT350NV2")
		set (OSCamOperatingSystem "OpenWRT")
		set (CS_OS_HW "wrt350nv2")
	elseif (OSCAM_SYSTEM_NAME MATCHES "Dockstar")
		set (OSCamOperatingSystem "OpenWRT")
		set (CS_OS_HW "dockstar")
	elseif (OSCAM_SYSTEM_NAME MATCHES "(OpenWRT|OpenWrt)")
		set (OSCamOperatingSystem "OpenWRT")
	elseif (OSCAM_SYSTEM_NAME MATCHES "MT7620")
		set (OSCamOperatingSystem "MT7620")
	elseif (OSCAM_SYSTEM_NAME MATCHES "Fonera2")
		set (OSCamOperatingSystem "Fonera2")
	elseif (OSCAM_SYSTEM_NAME MATCHES "DIR-825")
		set (OSCamOperatingSystem "DIR-825")
	elseif (OSCAM_SYSTEM_NAME MATCHES "A8PlusBox")
		set (OSCamOperatingSystem "A8PlusBox")
	elseif (OSCAM_SYSTEM_NAME MATCHES "AZBox")
		set (OSCamOperatingSystem "AZBox")
	elseif (OSCAM_SYSTEM_NAME MATCHES "SU980")
		set (OSCamOperatingSystem "SU980")
	elseif (OSCAM_SYSTEM_NAME MATCHES "MCA")
		set (OSCamOperatingSystem "MCA")
	elseif (OSCAM_SYSTEM_NAME MATCHES "agv2+w")
		set (OSCamOperatingSystem "agv2+w")
	elseif (OSCAM_SYSTEM_NAME MATCHES "WRT54G")
		set (OSCamOperatingSystem "WRT54G")
	elseif (OSCAM_SYSTEM_NAME MATCHES "Amino")
		set (OSCamOperatingSystem "Amino")
	elseif (OSCAM_SYSTEM_NAME MATCHES "QboxHD")
		set (OSCamOperatingSystem "QboxHD")
	elseif (OSCAM_SYSTEM_NAME MATCHES "Padre")
		set (OSCamOperatingSystem "Padre")
	elseif (OSCAM_SYSTEM_NAME MATCHES "FriendlyARM")
		set (OSCamOperatingSystem "FriendlyARM")
	elseif (OSCAM_SYSTEM_NAME MATCHES "Android")
		set (OSCamOperatingSystem "Android")
	elseif (${CYGWIN})
		set (OSCamOperatingSystem "Windows/Cygwin")
		set (CS_OS_HW "pc")
		set (CS_OS_SYS "cygwin")
		set (CMAKE_SYSTEM_PROCESSOR "i386")
	else (OSCAM_SYSTEM_NAME MATCHES "Tuxbox")
		message (STATUS "Unknown cross system name: <${CMAKE_SYSTEM_NAME}>")
		set (OSCamOperatingSystem "Unknown")
	endif (OSCAM_SYSTEM_NAME MATCHES "Tuxbox")
else (CMAKE_CROSSCOMPILING)
	if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
		set (OSCamOperatingSystem "Linux")
	elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
		set (OSCamOperatingSystem "FreeBSD")
	elseif (${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
		set (OSCamOperatingSystem "OpenBSD")
	elseif (OSCamOperatingSystem MATCHES "Padre")
		add_definitions ("-DPADRE -DSPARC")
		set (DEFAULT_CS_CONFDIR "/etc/oscam")
	elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
		set (OSCamOperatingSystem "Mac OS X")
		execute_process(COMMAND sw_vers -productVersion COMMAND cut -c 1-4 COMMAND tr -d '\n' OUTPUT_VARIABLE SWVER)
		execute_process(COMMAND xcodebuild -version COMMAND grep Xcode COMMAND sed "s/Xcode\ //g" COMMAND tr -d '\n' OUTPUT_VARIABLE XCODEVER)
		message (STATUS "mac sdk version is ${SWVER}")
		message (STATUS "mac Xcode version is ${XCODEVER}")
		add_definitions ("-w")
		set (CMAKE_OSX_DEPLOYMENT_TARGET "${SWVER}")
		if (SWVER GREATER 10.5)
			set (CMAKE_OSX_ARCHITECTURES "x86_64")
		endif (SWVER GREATER 10.5)
		if (SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
			message (STATUS "Compiling with xcode less then 4.3 on 10.7 If You have time update You're Xcode")
		endif (SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
		set (STATIC_LIBUSB True)
	elseif (${CYGWIN})
		set (OSCamOperatingSystem "Windows/Cygwin")
		set (CMAKE_SYSTEM_PROCESSOR "i386")
	else (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
		message (STATUS "Unknown system name: <${CMAKE_SYSTEM_NAME}>")
		set (OSCamOperatingSystem "Unknown")
	endif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
endif (CMAKE_CROSSCOMPILING)

#----------------------- some paths ------------------------------

set (OPTIONAL_LINK_DIR "" CACHE STRING "Some optional link directories")
set (OPTIONAL_INCLUDE_DIR "" CACHE STRING "Some optional include directories")

if (OSCamOperatingSystem MATCHES "Windows/Cygwin")
	include_directories (
		${CMAKE_CURRENT_SOURCE_DIR}/csctapi
		${CMAKE_CURRENT_SOURCE_DIR}/cscrypt
		${CMAKE_CURRENT_SOURCE_DIR}/minilzo
		${CMAKE_CURRENT_SOURCE_DIR}/ffdecsa
		${CMAKE_CURRENT_SOURCE_DIR}/extapi/cygwin
		/usr/include/w32api
		${OPTIONAL_INCLUDE_DIR}
		${OPTIONAL_INCLUDE_DIR}/w32api
		)
else (OSCamOperatingSystem MATCHES "Windows/Cygwin")
	include_directories (
		${CMAKE_CURRENT_SOURCE_DIR}/csctapi
		${CMAKE_CURRENT_SOURCE_DIR}/cscrypt
		${CMAKE_CURRENT_SOURCE_DIR}/minilzo
		${CMAKE_CURRENT_SOURCE_DIR}/ffdecsa
		${OPTIONAL_INCLUDE_DIR}
		)
endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")

if(OSCamOperatingSystem MATCHES "Windows/Cygwin")
	link_directories (
		/usr/lib
		/usr/lib/w32api
		/cygdrive/c/WINDOWS/system32/
		${OPTIONAL_LINK_DIR}
		)
else (OSCamOperatingSystem MATCHES "Windows/Cygwin")
	link_directories (
		${OPTIONAL_LINK_DIR}
		)
endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")

#----------------------- global options ------------------------------

if (OSCamOperatingSystem MATCHES "Linux")
	set (DEFAULT_CS_CONFDIR "/usr/local/etc")
	set (dl_link "dl")
elseif (OSCamOperatingSystem MATCHES "Mac OS X")
	if (NOT HAVE_PCSC EQUAL 0)
		add_definitions ("-DWITH_PCSC=1")
	endif (NOT HAVE_PCSC EQUAL 0)
	set (DEFAULT_CS_CONFDIR "/usr/local/etc")
elseif (OSCamOperatingSystem MATCHES "A8PlusBox")
	add_definitions ("-DWITH_TIARTOP=1")
	add_definitions ("-DD_FORTIFY_SOURCE=0")
	set (DEFAULT_CS_CONFDIR "/data/data/oscam")
elseif (OSCamOperatingSystem MATCHES "Android")
	set (DEFAULT_CS_CONFDIR "/data/data/oscam")
elseif (OSCamOperatingSystem MATCHES "Tuxbox")
	set (DEFAULT_CS_CONFDIR "/var/tuxbox/config")
	set (dl_link "dl")
elseif (OSCamOperatingSystem MATCHES "TripleDragon")
	add_definitions ("-DSTB04SCI=1")
	set (DEFAULT_CS_CONFDIR "/var/tuxbox/config")
	set (dl_link "dl")
elseif (OSCamOperatingSystem MATCHES "NeutrinoHD")
	add_definitions ("-DWITH_COOLAPI=1")
	set (USE_COOLAPI "USE_COOLAPI")
	set (DEFAULT_CS_CONFDIR "/var/tuxbox/config")
elseif (OSCamOperatingSystem MATCHES "NHD2")
	add_definitions ("-DWITH_COOLAPI2=1")
	set (USE_COOLAPI2 "USE_COOLAPI2")
	set (DEFAULT_CS_CONFDIR "/var/tuxbox/config")
elseif (OSCamOperatingSystem MATCHES "SlugOS")
	set (DEFAULT_CS_CONFDIR "/var/etc")
elseif (OSCamOperatingSystem MATCHES "OpenWRT")
	if (CS_OS_HW MATCHES "wrt350nv2")
		set (DEFAULT_CS_CONFDIR "/etc/oscam")
	elseif (CS_OS_HW MATCHES "dockstar")
		set (DEFAULT_CS_CONFDIR "/usr/oscam")
	else (CS_OS_HW MATCHES "wrt350nv2")
		set (DEFAULT_CS_CONFDIR "/etc/oscam")
	endif (CS_OS_HW MATCHES "wrt350nv2")
	set (dl_link "dl")
elseif (OSCamOperatingSystem MATCHES "MT7620")
	set (dl_link "dl")
	set (DEFAULT_CS_CONFDIR "/etc/oscam")
elseif (OSCamOperatingSystem MATCHES "Fonera2")
	set (DEFAULT_CS_CONFDIR "/var/etc")
elseif (OSCamOperatingSystem MATCHES "DIR-825")
	set (DEFAULT_CS_CONFDIR "/var/etc")
elseif (OSCamOperatingSystem MATCHES "AZBox")
	add_definitions ("-DWITH_AZBOX=1")
	set (USE_AZBOX "USE_AZBOX")
	set (DEFAULT_CS_CONFDIR "/PLUGINS/OpenXCAS/oscamCAS")
	set (dl_link "dl")
elseif (OSCamOperatingSystem MATCHES "SU980")
	set (USE_SU980 "USE_SU980")
	add_definitions ("-DWITH_SU980=1")
	set (DEFAULT_CS_CONFDIR "/var/tuxbox/config")
elseif (OSCamOperatingSystem MATCHES "MCA")
	add_definitions ("-DWITH_MCA=1")
	set (USE_MCA "USE_MCA")
	set (DEFAULT_CS_CONFDIR "/var/mca/d1")
elseif (OSCamOperatingSystem MATCHES "agv2+w")
	set (DEFAULT_CS_CONFDIR "/etc")
elseif (OSCamOperatingSystem MATCHES "WRT54G")
	set (DEFAULT_CS_CONFDIR "/jffs/etc/config/oscam")
elseif (OSCamOperatingSystem MATCHES "Amino")
	set (DEFAULT_CS_CONFDIR "/var/tuxbox/config")
elseif (OSCamOperatingSystem MATCHES "QboxHD")
	add_definitions ("-DQBOXHD=1")
	set (DEFAULT_CS_CONFDIR "/var/tuxbox/config")
elseif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
	add_definitions ("-static -DWITH_PCSC=1")
	set (DEFAULT_CS_CONFDIR ".")
elseif (OSCamOperatingSystem MATCHES "FreeBSD")
	add_definitions ("-I/usr/local/include -L/usr/local/lib")
	set (DEFAULT_CS_CONFDIR ".")
elseif (OSCamOperatingSystem MATCHES "OpenBSD")
	add_definitions ("-I/usr/local/include -L/usr/local/lib")
	set (DEFAULT_CS_CONFDIR "/etc/oscam")
elseif (OSCamOperatingSystem MATCHES "FriendlyARM")
	set (DEFAULT_CS_CONFDIR ".")
endif (OSCamOperatingSystem MATCHES "Linux")

set (CS_CONFDIR ${DEFAULT_CS_CONFDIR} CACHE STRING "Default path for the config files")
add_definitions ("-DCS_CONFDIR=\"${CS_CONFDIR}\"")

if (dl_link MATCHES "dl")
	add_definitions("-DWITH_DL")
	set(WITH_DL True)
else (dl_link MATCHES "dl")
	set(WITH_DL False)
endif (dl_link MATCHES "dl")

#----------------------- subdirectories ------------------------------

include (CheckIncludeFile)

check_include_file ("pthread.h" HAVE_PTHREAD)
if (HAVE_PTHREAD)
	message(STATUS "  pthread found. Adding pthread support ")
	add_definitions ("-DHAVE_PTHREAD_H")
elseif (HAVE_PTHREAD)
	message(STATUS "  no pthread found. No smartreader ")
endif (HAVE_PTHREAD)

check_include_file ("openssl/aes.h" HAVE_LIBCRYPTO)
if (HAVE_LIBCRYPTO)
	add_definitions ("-DWITH_LIBCRYPTO=1")
	set (USE_LIBCRYPTO "USE_LIBCRYPTO")
else (HAVE_LIBCRYPTO)
	message (STATUS " LIBCRYTO NOT FOUND OR DISABLED NO SSL SUPPORT POSSIBLE")
endif (HAVE_LIBCRYPTO)

if (USE_LIBCRYPTO AND STATIC_LIBCRYPTO AND LIBCRYPTODIR)
    if (EXISTS ${LIBCRYPTODIR}/lib/libcrypto.a)
        message(STATUS " static libcrypto found.")
        set(STATIC_LIBCRYPTO True)
        add_library(imp_libcrypto STATIC IMPORTED)
        set_property(TARGET imp_libcrypto PROPERTY IMPORTED_LOCATION ${LIBCRYPTODIR}/lib/libcrypto.a)
    else (EXISTS ${LIBCRYPTODIR}/lib/libcrypto.a)
        set(STATIC_LIBCRYPTO False)
    endif (EXISTS ${LIBCRYPTODIR}/lib/libcrypto.a)
else (USE_LIBCRYPTO AND STATIC_LIBCRYPTO AND LIBCRYPTODIR)
    set(STATIC_LIBCRYPTO False)
endif (USE_LIBCRYPTO AND STATIC_LIBCRYPTO AND LIBCRYPTODIR)

execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled WITH_SSL OUTPUT_VARIABLE CONFIG_WITH_SSL OUTPUT_STRIP_TRAILING_WHITESPACE)
if (CONFIG_WITH_SSL MATCHES "N" AND WITH_SSL EQUAL 1)
	message ( STATUS " ssl is added by You compiling with ssl included")
elseif (CONFIG_WITH_SSL MATCHES "Y" AND WITH_SSL EQUAL 1)
	message (STATUS " ssl is added by You but it was already enabled by config file")
elseif (CONFIG_WITH_SSL MATCHES "N" AND NOT WITH_SSL EQUAL 1)
	message (STATUS " SSL IS NOT INCLUDED IN THIS COMPILATION")
endif (CONFIG_WITH_SSL MATCHES "N" AND WITH_SSL EQUAL 1)

if (CONFIG_WITH_SSL MATCHES "Y" AND NOT WITH_SSL EQUAL 0)
	message(status "  SSL is added by config file ${CONFIG_WITH_SSL} compiling with ssl included")
	add_definitions ("-DWITH_SSL")
	set (WITH_SSL "1")
elseif (CONFIG_WITH_SSL MATCHES "Y" AND WITH_SSL EQUAL 0)
	message (status  " The config file has ssl enabled, but You disabled it by you cmake command COMPILING WHITOUT SSL")
endif (CONFIG_WITH_SSL MATCHES "Y" AND NOT WITH_SSL EQUAL 0)

find_package(OpenSSL)
if (OPENSSL_FOUND AND HAVE_LIBCRYPTO)
	include_directories(${OPENSSL_INCLUDE_DIR})
	message(STATUS "  openssl found.")
	if (WITH_SSL EQUAL 1)
		set(USE_SSL "USE_SSL")
	endif (WITH_SSL EQUAL 1)
else (OPENSSL_FOUND AND HAVE_LIBCRYPTO)
	message(STATUS "SSL REQUESTED BUT OPENSSL or LYBCRYPTO NOT FOUND !!")
	if (WITH_SSL)
		message(STATUS " ssl support requested but openssl or libcrypto NOT FOUND  COMPILING WHITOUT SSL !!! ")
		set (WITH_SSL "0")
	endif (WITH_SSL)
endif (OPENSSL_FOUND AND HAVE_LIBCRYPTO)

if (USE_SSL AND STATIC_LIBSSL AND LIBSSLDIR AND STATIC_LIBCRYPTO)
	if (EXISTS ${LIBSSLDIR}/lib/libssl.a)
		message(STATUS " static libssl found.")
		set(STATIC_SSL True)
		add_library(imp_libssl STATIC IMPORTED)
		set_property(TARGET imp_libssl PROPERTY IMPORTED_LOCATION ${LIBSSLDIR}/lib/libssl.a)
	else (EXISTS ${LIBSSLDIR}/lib/libssl.a)
		set(STATIC_SSL False)
	endif (EXISTS ${LIBSSLDIR}/lib/libssl.a)
else (USE_SSL AND STATIC_LIBSSL AND LIBSSLDIR AND STATIC_LIBCRYPTO)
    set(STATIC_SSL False)
endif (USE_SSL AND STATIC_LIBSSL AND LIBSSLDIR AND STATIC_LIBCRYPTO)

if (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android)")
	if (LIBRTDIR)
		check_include_file ("${LIBRTDIR}/include/sys/time.h" HAVE_LIBRT_STATIC)
		if (HAVE_LIBRT_STATIC)
			if (EXISTS ${LIBRTDIR}/lib/librt.a)
				message(STATUS "  static librt found : ${LIBRTDIR}/lib/librt.a (needed by libusb).")
				add_definitions ("-I${LIBRTDIR}/include/")
				add_library(imp_librt STATIC IMPORTED)
				set_property(TARGET imp_librt PROPERTY IMPORTED_LOCATION ${LIBRTDIR}/lib/librt.a)
				set(HAVE_LIBRT True)
			else (EXISTS ${LIBRTDIR}/lib/librt.a)
				message(STATUS " ${LIBRTDIR}/lib/librt.a not found (needed by libusb). No smartreader support)")
				set (HAVE_LIBRT_STATIC False)
			endif (EXISTS ${LIBRTDIR}/lib/librt.a)
		else (HAVE_LIBRT_STATIC)
			message(STATUS "  no librt found (needed by libusb). No smartreader support")
		endif (HAVE_LIBRT_STATIC)
	else (LIBRTDIR)
		check_include_file ("time.h" HAVE_LIBRT)
		if (HAVE_LIBRT)
			message(STATUS "  librt found (needed by libusb).")
		else (HAVE_LIBRT)
			message(STATUS "  no librt found (needed by libusb. No smartreader support")
		endif (HAVE_LIBRT)
	endif (LIBRTDIR)
	if (WITH_STAPI)
			if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi.a)
				message(STATUS "  liboscam_stapi.a found")
				add_definitions ("-DWITH_STAPI=1")
				add_library(stapilib STATIC IMPORTED)
				set_property(TARGET stapilib PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi.a)
				set (HAVE_LIBSTAPI True)
				set (USE_STAPI "USE_STAPI")
			else (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi.a)
				message(FATAL "  liboscam_stapi.a support requested but not found! ")
				set (HAVE_LIBSTAPI False)
			endif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi.a)
	endif (WITH_STAPI)
	if (WITH_STAPI5)
			if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi5.a)
				message(STATUS "  liboscam_stapi5.a found")
				add_definitions ("-DWITH_STAPI5=1")
				add_library(stapi5lib STATIC IMPORTED)
				set_property(TARGET stapi5lib PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi5.a)
				set (HAVE_LIBSTAPI5 True)
				set (USE_STAPI5 "USE_STAPI5")
			else (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi5.a)
				message(FATAL "  liboscam_stapi5.a support requested but not found! ")
				set (HAVE_LIBSTAPI5 False)
			endif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/stapi/liboscam_stapi5.a)
	endif (WITH_STAPI5)
else (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android)")
	set (HAVE_LIBRT True)
endif (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android)")


if( HAVE_LIBRT OR HAVE_LIBRT_STATIC)
	if (LIBUSBDIR)
		check_include_file ("${LIBUSBDIR}/include/libusb-1.0/libusb.h" HAVE_LIBUSB)
		if (HAVE_LIBUSB AND HAVE_PTHREAD)
			if (EXISTS ${LIBUSBDIR}/lib/libusb-1.0.a)
				if (STATIC_LIBUSB EQUAL 1)
					message(STATUS "  libusb 1.0 found (${LIBUSBDIR}/lib/libusb-1.0.a). Adding smartreader support ")
					add_definitions ("-DWITH_LIBUSB=1")
					add_definitions ("-I${LIBUSBDIR}/include/")
					set (LIBUSB "USE_LIBUSB")
					add_library(imp_libusb STATIC IMPORTED)
					set_property(TARGET imp_libusb PROPERTY IMPORTED_LOCATION ${LIBUSBDIR}/lib/libusb-1.0.a)
					set(LIBUSB_STATIC_LIBRARIES "${LIBUSBDIR}/lib/libusb-1.0.a")
				elseif (NOT STATIC_LIBUSB OR STATIC_LIBUSB EQUAL 0)
					set (CMAKE_FIND_ROOT_PATH ${LIBUSBDIR})
					find_library (LIBUSB_LIBRARY NAMES usb-1.0)
					message(STATUS "libusb 1.0 found (${LIBUSB_LIBRARY}). Adding smartreader support ")
					add_definitions ("-DWITH_LIBUSB=1")
					add_definitions ("-I${LIBUSBDIR}/include/")
					set (LIBUSB "USE_LIBUSB")
					add_library(imp_libusb SHARED IMPORTED)
					set_property(TARGET imp_libusb PROPERTY IMPORTED_LOCATION ${LIBUSB_LIBRARY} )
					set(LIBUSB_STATIC_LIBRARIES "${LIBUSB_LIBRARY}")
				endif (STATIC_LIBUSB EQUAL 1)
			else (EXISTS ${LIBUSBDIR}/lib/libusb-1.0.a)
					message(STATUS "  libusb 1.0 not found (${LIBUSBDIR}/lib/libusb). No smartreader support ")
					set (HAVE_LIBUSB False)
					set (HAVE_LIBRT False)
					set (HAVE_LIBRT_STATIC False)
			endif (EXISTS ${LIBUSBDIR}/lib/libusb-1.0.a)
		else (HAVE_LIBUSB AND HAVE_PTHREAD)
			message(STATUS "  no libusb 1.0 found. No smartreader support")
			set (HAVE_LIBUSB False)
			set (HAVE_LIBRT False)
			set (HAVE_LIBRT_STATIC False)
		endif (HAVE_LIBUSB AND HAVE_PTHREAD)
	else (LIBUSBDIR)
		if (NOT HAVE_LIBUSB EQUAL 0)
			check_include_file ("libusb-1.0/libusb.h" HAVE_LIBUSB)
			check_include_file ("libusb-1.0/libusb.h" PRESENT)
		endif(NOT HAVE_LIBUSB EQUAL 0)
		if (OSCamOperatingSystem MATCHES "Mac OS X")
			if (NOT HAVE_LIBUSB EQUAL 0 AND NOT PRESENT)
				find_file (USBINCL_LOC "libusb-1.0/libusb.h")
				if (EXISTS ${USBINCL_LOC})
					add_definitions ("-I/usr/local/include -L/usr/local/lib -I/usr/include -L/usr/lib")
					set (HAVE_LIBUSB True)
					set (PRESENT True)
					set (USE_LIBUSB "USE_LIBUSB")
					if (NOT STATIC_LIBUSB OR STATIC_LIBUSB EQUAL 0)
						set (STATICLIBUSB False)
					elseif (STATIC_LIBUSB EQUAL 1)
						set (STATICLIBUSB True)
					endif (NOT STATIC_LIBUSB OR STATIC_LIBUSB EQUAL 0)
				endif (EXISTS ${USBINCL_LOC})
			endif (NOT HAVE_LIBUSB EQUAL 0 AND NOT PRESENT)
		endif (OSCamOperatingSystem MATCHES "Mac OS X")
		if (NOT STATIC_LIBUSB OR STATIC_LIBUSB EQUAL 0)
			if(HAVE_LIBUSB AND HAVE_PTHREAD AND PRESENT)
				message(STATUS " libusb 1.0 found (libusb-1.0.so) Adding smartreader support ")
				add_definitions ("-DWITH_LIBUSB=1")
				set (USE_LIBUSB "USE_LIBUSB")
				set (STATICLIBUSB False)
			endif(HAVE_LIBUSB AND HAVE_PTHREAD AND PRESENT)
		elseif(STATIC_LIBUSB EQUAL 1)
			if (HAVE_LIBUSB AND HAVE_PTHREAD AND PRESENT)
				message(STATUS "  static libusb 1.0 found (libusb-1.0.a). Adding smartreader support ")
				add_definitions ("-DWITH_LIBUSB=1")
				set (USE_LIBUSB "USE_LIBUSB")
				set (STATICLIBUSB True)
			else (HAVE_LIBUSB AND HAVE_PTHREAD AND PRESENT)
				message(STATUS "  no libusb 1.0 found. No smartreader support")
				set (HAVE_LIBUSB False)
			endif (HAVE_LIBUSB AND HAVE_PTHREAD AND PRESENT)
		endif(NOT STATIC_LIBUSB OR STATIC_LIBUSB EQUAL 0)
	endif (LIBUSBDIR)
endif( HAVE_LIBRT OR HAVE_LIBRT_STATIC)

if (NOT WITH_PCSC EQUAL 0 OR HAVE_PCSC EQUAL 0)
	check_include_file ("PCSC/wintypes.h" HAVE_PCSC)
	if ((NOT HAVE_PCSC) AND OPTIONAL_INCLUDE_DIR)
		check_include_file("${OPTIONAL_INCLUDE_DIR}/PCSC/wintypes.h" HAVE_PCSC)
	endif((NOT HAVE_PCSC) AND OPTIONAL_INCLUDE_DIR)
else (NOT WITH_PCSC EQUAL 0 OR HAVE_PCSC EQUAL 0)
	set (HAVE_PCSC False)
endif (NOT WITH_PCSC EQUAL 0 OR HAVE_PCSC EQUAL 0)

if (HAVE_PCSC)
	FIND_PATH (PCSC_PATH PCSC/wintypes.h)
	if(OPTIONAL_INCLUDE_DIR AND EXISTS ${OPTIONAL_INCLUDE_DIR}/PCSC/wintypes.h)
		message(STATUS "  PCSC headers found (${OPTIONAL_INCLUDE_DIR}/PCSC). Adding PCSC support ")
		add_definitions ("-DWITH_PCSC=1")
		add_definitions ("-I${OPTIONAL_INCLUDE_DIR}/PCSC")
		set (USE_PCSC "USE_PCSC")
	elseif (EXISTS ${PCSC_PATH}/PCSC/wintypes.h AND NOT OPTIONAL_INCLUDE_DIR)
		message(STATUS "  PCSC headers found (${PCSC_PATH}/PCSC). Adding PCSC support ")
		add_definitions ("-DWITH_PCSC=1")
		add_definitions ("-I${PCSC_PATH}/PCSC")
		set (USE_PCSC "USE_PCSC")
	else (OPTIONAL_INCLUDE_DIR AND EXISTS ${OPTIONAL_INCLUDE_DIR}/PCSC/wintypes.h)
		if (NOT OSCamOperatingSystem MATCHES "Mac OS X")
			set (HAVE_PCSC False)
			if(OPTIONAL_INCLUDE_DIR)
				message(STATUS "  PCSC headers not found (not in /usr/include/PCSC or /usr/local/include/PCSC or ${OPTIONAL_INCLUDE_DIR}/PCSC). No PCSC support ")
			else(OPTIONAL_INCLUDE_DIR)
				message(STATUS "  PCSC headers not found (not in /usr/include/PCSC or /usr/local/include/PCSC). No PCSC support ")
			endif(OPTIONAL_INCLUDE_DIR)
		endif (NOT OSCamOperatingSystem MATCHES "Mac OS X")
	endif(OPTIONAL_INCLUDE_DIR AND EXISTS ${OPTIONAL_INCLUDE_DIR}/PCSC/wintypes.h)
	if (OSCamOperatingSystem MATCHES "Mac OS X" AND NOT HAVE_PCSC EQUAL 0)
		set (USE_PCSC "USE_PCSC")
		set (HAVE_PCSC "1")
	endif (OSCamOperatingSystem MATCHES "Mac OS X" AND NOT HAVE_PCSC EQUAL 0)
else (HAVE_PCSC)
	if (OSCamOperatingSystem MATCHES "Windows/Cygwin")
		add_definitions ("-Icygwin")
		set (HAVE_PCSC "1")
		set (USE_PCSC "USE_PCSC")
	endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
endif (HAVE_PCSC)

if (HAVE_PCSC AND USE_PCSC)
    if (NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
        if (STATIC_LIBPCSC EQUAL 1)
            if (EXISTS ${LIBPCSCDIR}/lib/libpcsclite.a)
                 message(STATUS "  libpcsclite found (${LIBPCSCDIR}/lib/libpcsclite.a). Adding smartreader support ")
                 add_library(imp_libpcsclite STATIC IMPORTED)
                 set_property(TARGET imp_libpcsclite PROPERTY IMPORTED_LOCATION ${LIBPCSCDIR}/lib/libpcsclite.a)
                 set (STATIC_LIBPCSC True)
                 set (libpcsc_link "imp_libpcsclite")
                 add_definitions("-DSTATIC_LIBPCSC")
            else (EXISTS ${LIBPCSCDIR}/lib/libpcsclite.a)
                set(STATIC_LIBPCSC False)
                if (NOT WITH_DL)
                    set(libpcsc_link pcsclite)
                endif (NOT WITH_DL)
             endif (EXISTS ${LIBPCSCDIR}/lib/libpcsclite.a)
        else (STATIC_LIBPCSC EQUAL 1)
            set(STATIC_LIBPCSC False)
            if (NOT WITH_DL)
                set(libpcsc_link pcsclite)
            endif (NOT WITH_DL)
        endif (STATIC_LIBPCSC EQUAL 1)
    endif (NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
else (HAVE_PCSC AND USE_PCSC)
    set (STATIC_LIBPCSC False)
endif (HAVE_PCSC AND USE_PCSC)


# Manage config.h based on command line parameters
# Manipulate config file based on given parameters and read unset parameters

execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled WITH_EMU OUTPUT_VARIABLE CONFIG_WITH_EMU OUTPUT_STRIP_TRAILING_WHITESPACE)
if (CONFIG_WITH_EMU MATCHES "Y" AND NOT WITH_EMU EQUAL 1)
	add_definitions ("-DWITH_EMU")
	set (WITH_EMU "1")
	message (STATUS "  EMU is added by config compiling with EMU")
endif (CONFIG_WITH_EMU MATCHES "Y" AND NOT WITH_EMU EQUAL 1)

if (WITH_EMU)
	execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled WITH_SOFTCAM OUTPUT_VARIABLE CONFIG_WITH_SOFTCAM OUTPUT_STRIP_TRAILING_WHITESPACE)
	if (CONFIG_WITH_SOFTCAM MATCHES "Y" AND NOT WITH_SOFTCAM EQUAL 1)
		add_definitions ("-DWITH_SOFTCAM")
		set (WITH_SOFTCAM "1")
		message (STATUS "  SOFTCAM is added by config linking SoftCam.Key")
	endif (CONFIG_WITH_SOFTCAM MATCHES "Y" AND NOT WITH_SOFTCAM EQUAL 1)
endif (WITH_EMU)

execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --show-valid OUTPUT_VARIABLE config_vars_string OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REGEX MATCHALL "[A-Z0-9_]+" config_vars ${config_vars_string})

MACRO(GENERATE_OSCAM_CONFIG fullpath)
	foreach(option ${config_vars})
		if(DEFINED ${option})
			if(${option})
				execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enable ${option})
			else(${option})
				execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --disable ${option})
			endif(${option})
		endif(DEFINED ${option})
	endforeach(option)
ENDMACRO(GENERATE_OSCAM_CONFIG fullpath)

GENERATE_OSCAM_CONFIG("${CMAKE_CURRENT_SOURCE_DIR}/config.h")

# Build config.mak and related files
# FIXME: Create USE_FLAGS and pass them to config.sh using --use-flags
execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --objdir ${CMAKE_CURRENT_BINARY_DIR} --use-flags  "${USE_STAPI} ${USE_STAPI5} /
${USE_COOLAPI} ${USE_SU980} ${USE_AZBOX} ${USE_MCA} ${USE_SSL} ${USE_LIBCRYPTO} ${USE_LIBUSB} ${USE_PCSC}" --make-config.mak)
# Generate webif/pages.c
execute_process (COMMAND make --no-print-directory --quiet -C ${CMAKE_CURRENT_SOURCE_DIR}/webif)

#----------------------- subdirectories ------------------------------

add_subdirectory (csctapi)
add_subdirectory (minilzo)
add_subdirectory (cscrypt)
add_subdirectory (ffdecsa)

#----------------------- file groups ------------------------------
execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled MODULE_CAMD33 OUTPUT_VARIABLE CAMD33 OUTPUT_STRIP_TRAILING_WHITESPACE)
if (CAMD33 MATCHES "N")
set (CAMD_33 "module-camd33*")
endif (CAMD33 MATCHES "N")
if (OSCamOperatingSystem MATCHES "Mac OS X")
file (GLOB csmodules_mac  "module-dvbapi*" "module-lcd*" "${CAMD_33}")
file (GLOB csmodules_srcs "module-*.c" "webif/pages.c")
list (REMOVE_ITEM csmodules_srcs ${csmodules_mac})
file (GLOB csmodules_hdrs "module-*.h" "webif/pages.h")
list (REMOVE_ITEM csmodules_hdrs ${csmodules_mac})
file (GLOB csreaders_srcs "reader-*.c")
file (GLOB csreaders_hdrs "reader-*.h")
file (GLOB csoscam_srcs "oscam-*.c")
file (GLOB csoscam_hdrs "oscam-*.h")
file (GLOB exe_srcs "oscam.c")
file (GLOB exe_hdrs "globals.h")
file (GLOB all_srcs ${csmodules_srcs} ${csreaders_srcs} ${csoscam_srcs} ${exe_srcs})
else (OSCamOperatingSystem MATCHES "Mac OS X")
file (GLOB csmodules_srcs "module-*.c" "webif/pages.c")
file (GLOB csmodules_hdrs "module-*.h" "webif/pages.h")
file (GLOB csreaders_srcs "reader-*.c")
file (GLOB csreaders_hdrs "reader-*.h")
    if (OSCamOperatingSystem MATCHES "A8PlusBox")
        file (GLOB csoscam_srcs "oscam-*.c" "${CMAKE_CURRENT_BINARY_DIR}/config.c" "extapi/a8plus_box/config.c")
        file (GLOB csoscam_hdrs "oscam-*.h")
    elseif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
        file (GLOB csoscam_srcs "oscam-*.c" "${CMAKE_CURRENT_BINARY_DIR}/config.c" "extapi/cygwin/*.c")
        file (GLOB csoscam_hdrs "oscam-*.h" "extapi/cygwin/*.h")
    else (OSCamOperatingSystem MATCHES "A8PlusBox")
        file (GLOB csoscam_srcs "oscam-*.c" "${CMAKE_CURRENT_BINARY_DIR}/config.c")
        file (GLOB csoscam_hdrs "oscam-*.h")
    endif (OSCamOperatingSystem MATCHES "A8PlusBox")
file (GLOB exe_srcs "oscam.c")
file (GLOB exe_hdrs "globals.h")
file (GLOB all_srcs ${csmodules_srcs} ${csreaders_srcs} ${csoscam_srcs} ${exe_srcs})
endif (OSCamOperatingSystem MATCHES "Mac OS X")
#----------------------- modules ------------------------------

set (csmodules "csmodules")
add_library (${csmodules} STATIC ${csmodules_srcs} ${csmodules_hdrs})

#----------------------- readers ------------------------------

set (csreaders "csreaders")
add_library (${csreaders} STATIC ${csreaders_srcs} ${csreaders_hdrs})

#----------------------- other oscam files ------------------------------

set (csoscam "csoscam")
add_library (${csoscam} STATIC ${csoscam_srcs} ${csoscam_hdrs})


#----------------------- the executable ------------------------------

set (exe_name "oscam")
add_executable (${exe_name} ${exe_srcs} ${exe_hdrs})
target_link_libraries (${exe_name} ${csoscam} ${csmodules} ${csreaders} csctapi cscrypt minilzo ffdecsa)
if(HAVE_LIBRT AND HAVE_LIBUSB)
	if (LIBUSBDIR)
		set (libusb_link "imp_libusb")
	else(LIBUSBDIR)
		if (STATICLIBUSB)
			set (libusb_link "libusb-1.0.a")
		else (STATICLIBUSB)
			set (libusb_link "usb-1.0")
		endif(STATICLIBUSB)
	endif(LIBUSBDIR)
	if (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android|A8PlusBox)")
        if (HAVE_LIBRT_STATIC AND STATIC_LIBRT EQUAL 1)
			set (rt_link "imp_librt")
		else(HAVE_LIBRT_STATIC AND STATIC_LIBRT EQUAL 1)
			set (rt_link "rt")
		endif(HAVE_LIBRT_STATIC AND STATIC_LIBRT EQUAL 1)
	endif (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android|A8PlusBox)")
	if (OSCamOperatingSystem MATCHES "Windows/Cygwin")
		set (setupapi_link "setupapi")
		set (ole32_link "ole32")
		set (shell32_link "shell32")
	endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
endif(HAVE_LIBRT AND HAVE_LIBUSB)

if (HAVE_LIBRT_STATIC AND HAVE_LIBUSB)
	if (LIBUSBDIR)
		set (libusb_link "imp_libusb")
	else(LIBUSBDIR)
		if (STATICLIBUSB)
			set (libusb_link "libusb-1.0.a")
		else (STATICLIBUSB)
			set (libusb_link "usb-1.0")
		endif(STATICLIBUSB)
	endif(LIBUSBDIR)
	if (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android|A8PlusBox)")
		set (rt_link "imp_librt")
	endif (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android|A8PlusBox)")
	if (OSCamOperatingSystem MATCHES "Windows/Cygwin")
		set (setupapi_link "setupapi")
		set (ole32_link "ole32")
		set (shell32_link "shell32")
	endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
endif (HAVE_LIBRT_STATIC AND HAVE_LIBUSB)

if (HAVE_PTHREAD)
	if (OSCamOperatingSystem MATCHES "Windows/Cygwin")
		set (pthread_link "pthread")
    else (OSCamOperatingSystem MATCHES "Windows/Cygwin")
	    set (pthread_link "-pthread")
    endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
endif (HAVE_PTHREAD)

if (WITH_SSL)
	if (STATIC_LIBSSL AND NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
		set(ssl_link "imp_libssl")
	else (STATIC_LIBSSL AND NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
		target_link_libraries (${exe_name} ssl)
	endif (STATIC_LIBSSL AND NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
endif (WITH_SSL)

if (HAVE_LIBCRYPTO)
	if (STATIC_LIBCRYPTO)
		set(crypto_link "imp_libcrypto")
	else (STATIC_LIBCRYPTO)
		target_link_libraries (${exe_name} crypto)
	endif (STATIC_LIBCRYPTO)
endif (HAVE_LIBCRYPTO)

# some systems need libm for the nagra cak7 functions to work
check_include_file("math.h" HAVE_LIBM)
if(HAVE_LIBM)
	target_link_libraries (${exe_name} m)
endif(HAVE_LIBM)

if (HAVE_PCSC AND NOT WITH_DL)
if (NOT OSCamOperatingSystem MATCHES "Mac OS X")
if (NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
	target_link_libraries (${exe_name} pcsclite)
endif (NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
endif (NOT OSCamOperatingSystem MATCHES "Mac OS X")
endif (HAVE_PCSC AND NOT WITH_DL)

if (HAVE_LIBSTAPI)
	target_link_libraries(${exe_name} stapilib)
endif (HAVE_LIBSTAPI)

if (HAVE_LIBSTAPI5)
	target_link_libraries(${exe_name} stapi5lib)
endif (HAVE_LIBSTAPI5)

add_dependencies (${exe_name} ${csoscam} ${csreaders} ${csmodules})

#----------------------- specific options ------------------------------

if (NOT STATIC_LIBRT EQUAL 1 AND NOT OSCamOperatingSystem MATCHES "(Mac OS X|Windows/Cygwin|FreeBSD|OpenBSD|Android|A8PlusBox)")
	target_link_libraries (${exe_name} rt)
endif (NOT STATIC_LIBRT EQUAL 1 AND NOT OSCamOperatingSystem MATCHES "(Mac OS X|Windows/Cygwin|FreeBSD|OpenBSD|Android|A8PlusBox)")

if (OSCamOperatingSystem MATCHES "Linux")
elseif (OSCamOperatingSystem MATCHES "Mac OS X")
	if (NOT HAVE_PCSC EQUAL 0)
		FIND_LIBRARY (PCSC_LIBRARY PCSC)
		message (STATUS "PCSC Mac OS X found : ${PCSC_LIBRARY}")
		add_definitions ("-DWITH_PCSC=1")
		target_link_libraries ( ${exe_name} ${PCSC_LIBRARY})
		set (HAVE_PCSC True)
		FIND_LIBRARY (IOKit_LIBRARY IOKit)
		target_link_libraries ( ${exe_name} ${IOKit_LIBRARY})
		FIND_LIBRARY (CoreFoundation_LIBRARY CoreFoundation)
		target_link_libraries ( ${exe_name} ${CoreFoundation_LIBRARY})
	else (NOT HAVE_PCSC EQUAL 0)
		message (STATUS "PCSC Mac OS X not found no pcsc support")
	endif (NOT HAVE_PCSC EQUAL 0)
elseif (OSCamOperatingSystem MATCHES "Tuxbox")
elseif (OSCamOperatingSystem MATCHES "TripleDragon")
elseif (OSCamOperatingSystem MATCHES "MT7620")
elseif (OSCamOperatingSystem MATCHES "SlugOS")
elseif (OSCamOperatingSystem MATCHES "OpenWRT")
elseif (OSCamOperatingSystem MATCHES "Fonera2")
elseif (OSCamOperatingSystem MATCHES "DIR-825")
elseif (OSCamOperatingSystem MATCHES "agv2+w")
elseif (OSCamOperatingSystem MATCHES "WRT54G")
elseif (OSCamOperatingSystem MATCHES "Amino")
elseif (OSCamOperatingSystem MATCHES "QboxHD")
elseif (OSCamOperatingSystem MATCHES "Windows/Cygwin")
	message (STATUS "Windows system PCSC : winscard.dll")
	target_link_libraries (${exe_name} -Wl,--enable-stdcall-fixup winscard)
	target_link_libraries (${exe_name} setupapi )
	target_link_libraries (${exe_name} ole32)
	target_link_libraries (${exe_name} shell32)
elseif (OSCamOperatingSystem MATCHES "NeutrinoHD")
	target_link_libraries (${exe_name} rt nxp)
elseif (OSCamOperatingSystem MATCHES "NHD2")
	target_link_libraries (${exe_name} lnxcssUsr lnxscsUsr lnxnotifyqUsr lnxUKAL lnxplatUsr rt)
elseif (OSCamOperatingSystem MATCHES "A8PlusBox")
	add_library(xcam STATIC IMPORTED)
	set_property(TARGET xcam PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/extapi/a8plus_box/libA8PlusXCam.a)
	set (xcam_link "xcam")
	target_link_libraries (${exe_name} -L${CMAKE_CURRENT_SOURCE_DIR}/extapi/a8plus_box hi_msp hi_common)
elseif (OSCamOperatingSystem MATCHES "AZBox")
	add_library(xcas STATIC IMPORTED)
	set_property(TARGET xcas PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/extapi/openxcas/libOpenXCASAPI.a)
	set (xcas_link "xcas")
elseif (OSCamOperatingSystem MATCHES "SU980")
	target_link_libraries (${exe_name} rt entropic)
endif (OSCamOperatingSystem MATCHES "Linux")

target_link_libraries (${exe_name} ${libusb_link} ${rt_link} ${setupapi_link} ${ole32_link} ${shell32_link} ${pthread_link} ${dl_link} ${xcas_link} ${libpcsc_link} ${ssl_link} ${crypto_link} ${xcam_link})

#----------------------- put svnversion in the build ------------------------------
# at every target rebuild, we re-build the oscam.c compilation...

# add the needed dependency
add_dependencies (${exe_name} svnversion)
add_dependencies (${csoscam} svnversion-log)

# how to re-build the files which printout the version
add_custom_target (
	svnversion
	ALL
	#touch ${CMAKE_CURRENT_SOURCE_DIR}/oscam.c
)
add_custom_target (
	svnversion-log
	ALL
	#touch ${CMAKE_CURRENT_SOURCE_DIR}/oscam-log.c
)

execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --oscam-revision COMMAND tr -d '\n' OUTPUT_VARIABLE CS_SVN_VERSION)
add_definitions ("-D'CS_SVN_VERSION=\"${CS_SVN_VERSION}\"'")

execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpmachine COMMAND tr -d '\n' OUTPUT_VARIABLE CS_TARGET)
add_definitions ("-D'CS_TARGET=\"${CS_TARGET}\"'")
#----------------------- global compile and link options ------------------------------
#enable sse2 on x86
if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse -msse2 -msse3")
endif (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")

# disable warning about unused but set variables in gcc 4.6+
if (CMAKE_COMPILER_IS_GNUCC)
	execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
	string(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION})
	list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
	list(GET GCC_VERSION_COMPONENTS 0 GCC_MINOR)
	add_definitions ("-W -Wall ")
	set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
	set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O2")
	set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -ggdb")
	set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -ggdb")
endif (CMAKE_COMPILER_IS_GNUCC)
# some optimisations
if (OSCamOperatingSystem MATCHES "Mac OS X")
	if (SWVER GREATER 10.6)
		if (SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
			add_definitions("-isysroot /Developer/SDKs/MacOSX${SWVER}.sdk")
		else (SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
			add_definitions("-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${SWVER}.sdk")
		endif (SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
	elseif(SWVER LESS 10.7)
		add_definitions("-isysroot /Developer/SDKs/MacOSX${SWVER}.sdk")
	endif (SWVER GREATER 10.6)
endif (OSCamOperatingSystem MATCHES "Mac OS X")

# we don't want the '-rdynamic' in the link command
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")

# we want to strip if not Debug build
if (OSCamOperatingSystem MATCHES "Mac OS X")
	if (SWVER MATCHES "10.8")
		if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				set (CMAKE_EXE_LINKER_FLAGS "-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${SWVER}.sdk")
		else (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				message (STATUS "  building with debug")
				set (CMAKE_EXE_LINKER_FLAGS "-ggdb -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${SWVER}.sdk")
		endif (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
	elseif (SWVER MATCHES "10.7")
		if (SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
			if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				set (CMAKE_EXE_LINKER_FLAGS "-isysroot /Developer/SDKs/MacOSX${SWVER}.sdk")
			else (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				message (STATUS "  building with debug")
				set (CMAKE_EXE_LINKER_FLAGS "-ggdb -isysroot /Developer/SDKs/MacOSX${SWVER}.sdk")
			endif (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
		else(SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
			if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				set (CMAKE_EXE_LINKER_FLAGS "-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${SWVER}.sdk")
			else (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				message (STATUS "  building with debug")
				set (CMAKE_EXE_LINKER_FLAGS "-ggdb -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${SWVER}.sdk")
			endif (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
		endif(SWVER EQUAL 10.7 AND XCODEVER LESS 4.3)
	elseif (SWVER MATCHES "10.6")
		if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				set (CMAKE_EXE_LINKER_FLAGS "-isysroot /Developer/SDKs/MacOSX${SWVER}.sdk")
		else (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				message (STATUS "  building with debug")
				set (CMAKE_EXE_LINKER_FLAGS "-ggdb -isysroot /Developer/SDKs/MacOSX${SWVER}.sdk")
		endif (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
	endif (SWVER MATCHES "10.8")
else (OSCamOperatingSystem MATCHES "Mac OS X")
		if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				set (CMAKE_EXE_LINKER_FLAGS "-s")
		else (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
				message (STATUS "  building with debug")
				set (CMAKE_EXE_LINKER_FLAGS "-ggdb")
		endif (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
endif (OSCamOperatingSystem MATCHES "Mac OS X")

if (OSCamOperatingSystem MATCHES "Windows/Cygwin")
	set (CMAKE_EXE_LINKER_FLAGS "--enable-stdcall-fixup")
endif (OSCamOperatingSystem MATCHES "Windows/Cygwin")

if (OSCamOperatingSystem MATCHES "Android")
    set (CMAKE_C_FLAGS "-pie -fPIE")
    set (CMAKE_EXE_LINKER_FLAGS "-pie -fPIE")
endif (OSCamOperatingSystem MATCHES "Android")

#----------------------- subdirectory list smargo ------------------------------

if( HAVE_LIBRT OR HAVE_LIBRT_STATIC)
	if (HAVE_LIBUSB)
		add_subdirectory (utils)
	endif (HAVE_LIBUSB)
endif( HAVE_LIBRT OR HAVE_LIBRT_STATIC)

#------------------------------------------------------------------------

if (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android)")
if (NOT DEFINED ENV{ANDROID_NDK})
if (NOT DEFINED ENV{ANDROID_STANDALONE_TOOLCHAIN})
	if (WITH_SOFTCAM)
		if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
				execute_process (COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key ${CMAKE_CURRENT_BINARY_DIR}/SoftCam.Key)
		else (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
				execute_process (COMMAND touch ${CMAKE_CURRENT_BINARY_DIR}/SoftCam.Key)
		endif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
		set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--format=binary -Wl,SoftCam.Key -Wl,--format=default")
	endif (WITH_SOFTCAM)
endif (NOT DEFINED ENV{ANDROID_STANDALONE_TOOLCHAIN})
endif (NOT DEFINED ENV{ANDROID_NDK})
endif (NOT OSCamOperatingSystem MATCHES "(Mac OS X|Android)")

#----------------------- installation -----------------------------

file (GLOB config_files "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/oscam.*")
file (GLOB doc_files "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/txt/*.txt")
file (GLOB man1_files "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/man/*.1")
file (GLOB man5_files "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/man/*.5")
file (GLOB example_files "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.ac"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.cert"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.conf"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.guess"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.ird"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.server"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.services"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.srvid"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/doc/example/oscam.user")
file (GLOB monitor_files "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/monitor/mpcs*.sh"
						 "${CMAKE_CURRENT_SOURCE_DIR}/Distribution/monitor/mpcs*.tar")

install (PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${exe_name}${CMAKE_EXECUTABLE_SUFFIX} DESTINATION bin COMPONENT bin)
install (FILES ${config_files} DESTINATION etc COMPONENT config)
install (FILES ${doc_files} DESTINATION share/doc/oscam COMPONENT doc)
install (FILES ${man1_files} DESTINATION share/man/man1 COMPONENT doc)
install (FILES ${man5_files} DESTINATION share/man/man5 COMPONENT doc)
install (FILES ${example_files} DESTINATION share/doc/oscam/example COMPONENT doc)
install (FILES ${monitor_files} DESTINATION share/doc/oscam/monitor COMPONENT doc)

#----------------------- we can use CPack to build pacakges --------------------------

# get version from source file
execute_process (COMMAND grep CS_VERSION ${CMAKE_CURRENT_SOURCE_DIR}/globals.h
				 OUTPUT_VARIABLE CS_VERSION)
string (REGEX REPLACE ".*\"\(.*\)\".*" "\\1" CS_VERSION ${CS_VERSION})
string (REGEX REPLACE "\(.*\)\\..*\\..*" "\\1" CPACK_PACKAGE_VERSION_MAJOR ${CS_VERSION})
string (REGEX REPLACE ".*\\.\(.*\)\\..*" "\\1" CPACK_PACKAGE_VERSION_MINOR ${CS_VERSION})
string (REGEX REPLACE ".*\\..*\\.\(.*\)" "\\1" CPACK_PACKAGE_VERSION_PATCH ${CS_VERSION})
set (CPACK_PACKAGE_VERSION
	 "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")

# compute my filename
set (CPACK_PACKAGE_FILE_NAME
	 "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION}-${CS_TARGET}")

# wich archive we want to generate
set (CPACK_GENERATOR "TGZ")

# what to put in the archive
set (CPACK_STRIP_FILES TRUE)
set (CPACK_COMPONENTS_ALL bin)# config doc)

# ok, we can now use CPack
include (CPack)

#----------------------- printout resume -----------------------------

message (STATUS "")
message (STATUS "  operating system: ${OSCamOperatingSystem}")
message (STATUS "  target system: ${CS_TARGET}")
message (STATUS "  revision: ${CS_SVN_VERSION}")
if (HAVE_LIBCRYPTO)
	if (STATIC_LIBCRYPTO)
		message (STATUS "  use static libcrypto.")
	else (STATIC_LIBCRYPTO)
		message (STATUS "  use system libcrypto functions")
	endif (STATIC_LIBCRYPTO)
else (HAVE_LIBCRYPTO)
	message (STATUS "  use built-in crypt functions")
endif (HAVE_LIBCRYPTO)
if (HAVE_PCSC)
	if (STATIC_LIBPCSC)
		message (STATUS "  use static pcsc functions")
	else (STATIC_LIBPCSC)
		message (STATUS "  use system pcsc functions")
	endif (STATIC_LIBPCSC)
endif (HAVE_PCSC)
if (HAVE_PTHREAD)
	message (STATUS "  use system pthread functions")
endif (HAVE_PTHREAD)
if (HAVE_LIBUSB)
if(STATIC_LIBUSB EQUAL 1)
	message (STATUS "  You selected to enable static libusb")
endif(STATIC_LIBUSB EQUAL 1)
	if(STATICLIBUSB AND NOT LIBUSBDIR)
		message (STATUS "  use static libusb functions")
	else(STATICLIBUSB AND NOT LIBUSBDIR)
		if (LIBUSBDIR AND STATIC_LIBUSB EQUAL 0)
			message(STATUS "  use system libusb from selected LIBUSBDIR functions")
		elseif (LIBUSBDIR AND STATIC_LIBUSB EQUAL 1)
			message(STATUS "  use static libusb from selected LIBUSBDIR functions")
		elseif(LIBUSBDIR)
			message(STATUS " use system libusb from selected LIBUSBDIR functions")
		elseif(NOT LIBUSBDIR AND NOT STATIC_LIBUSB)
			message (STATUS "  use system libusb functions")
		endif(LIBUSBDIR AND STATIC_LIBUSB EQUAL 0)
	endif(STATICLIBUSB AND NOT LIBUSBDIR)
endif (HAVE_LIBUSB)

if (WITH_EMU)
	message (STATUS "  Compile with EMU support")
	if (WITH_SOFTCAM)
		message (STATUS "  SoftCam.Key will be linked as well")
	endif (WITH_SOFTCAM)
endif (WITH_EMU)

message (STATUS "")
